package com.yfbao.horizon.demo;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.yfbao.horizon.utils.FileLocalUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AccessLogAnalysis {

    public static void main(String[] args) {
        String src ="C:\\Users\\byf78\\Desktop\\日志分析\\dev\\access_log.2025-02-27.log";


        doAnalysis(src);

//        doAnalysisByTime(src);
    }


    /**
     * 关键字过滤分析
     * @param file
     */
    private static void doAnalysis(String file){
        String outFile = "C:\\Users\\byf78\\Desktop\\日志分析\\dev\\3.log";
        String fifter ="POST /\"";
//        String fifter ="healthCheck";
//        String fifter ="404";
//        List<String> stringList = FileLocalUtils.fifterLog(file, null);
        List<String> stringList = FileLocalUtils.fifterLog(file, fifter);
        List<String> res = new ArrayList<>();
        for (String s : stringList) {
            String[] split = s.split(" ");
            int length = split.length;
            String last = split[length - 1];
            String status = split[length - 3];
            try {
                Integer integer = Integer.valueOf(last);
                if(integer>1000){
                    res.add(s);
                }
                Integer stat = Integer.valueOf(status);
                if(stat == 404){
                    res.add(s);
                }
            }catch (Exception e){
                System.out.println(last+" parse to int error");
            }
        }

        System.out.println("----finish analysis------------------------------------");
        for (String re : res) {
            System.out.println(re);
        }
        FileUtil.writeLines(res,outFile,"utf-8");
    }

    /**
     * 关键时间段过滤分析
     * @param file
     */
    private static void doAnalysisByTime(String file){
        String outFile = "C:\\Users\\byf78\\Desktop\\日志分析\\稳定环境\\0926宕机\\org0926-access-out-tt2.log";
//        String fifter ="select-front-nav-menu";
        String fifter ="healthCheck";
        DateTime start = DateUtil.parse("2023-09-26 09:10:00", "yyyy-MM-dd hh:mm:ss");
        DateTime end = DateUtil.parse("2023-09-26 11:01:00", "yyyy-MM-dd hh:mm:ss");
//        String fifter ="404";
//        List<String> stringList = FileLocalUtils.fifterLog(file, null);
        List<String> stringList = FileLocalUtils.fifterLogBetweenTime(file, start.getTime(),end.getTime());
        List<String> res = new ArrayList<>();
        for (String s : stringList) {
            String[] split = s.split(" ");
            int length = split.length;
            String last = split[length - 1];
            String status = split[length - 3];
            try {
                Integer integer = Integer.valueOf(last);
                if(integer>1000){
                    res.add(s);
                }
                Integer stat = Integer.valueOf(status);
                if(stat == 404){
                    res.add(s);
                }
            }catch (Exception e){
                System.out.println(last+" parse to int error");
            }
        }

        System.out.println("----finish analysis------------------------------------");
        for (String re : res) {
            System.out.println(re);
        }
        FileUtil.writeLines(res,outFile,"utf-8");
    }
}
